/**
 * Copyright (C) 2013 DaiKit.com - daikit-commons module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.commons.shared.exception;

import com.daikit.commons.shared.exception.context.DkExceptionContext;



/**
 * Exception that all Daikit exceptions should extend.
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class DkException extends RuntimeException
{

	private static final long serialVersionUID = 4114937949535618292L;

	private DkExceptionContext context;

	/**
	 * Message for errors wrapped because not serializable through GWT rpcs.
	 */
	public static final String UNKNWOWN_ERROR_MESSAGE = "unknwown_error";

	/**
	 * Default Constructor, create a Context that can be accessed by {@link #getContext()} , the message is
	 * {@link #UNKNWOWN_ERROR_MESSAGE}
	 */
	public DkException()
	{
		super(UNKNWOWN_ERROR_MESSAGE);
		this.context = new DkExceptionContext();
	}

	/**
	 * Constructs a new GwtLib exception with given message. create a Context that can be accessed by
	 * {@link #getContext()}.
	 * 
	 * @param message
	 *           the message for the exception
	 */
	public DkException(final String message)
	{
		super(message);
		this.context = new DkExceptionContext();
	}

	/**
	 * Constructs a new GwtLib exception with <code>null</code> as its detail message. The cause is not initialized, and
	 * may subsequently be initialized by a call to {@link #initCause}.
	 * 
	 * @param context
	 *           the context when exception occurred
	 */
	public DkException(final DkExceptionContext context)
	{
		super(context.getMessage());
		this.context = context;
	}

	/**
	 * Constructs a new GwtLib exception with the specified cause and a detail message of
	 * <tt>(cause==null ? null : cause.toString())</tt> (which typically contains the class and detail message of
	 * <tt>cause</tt>). This constructor is useful for runtime exceptions that are little more than wrappers for other
	 * throwables.
	 * 
	 * @param context
	 *           the context when exception occurred
	 * @param cause
	 *           the cause (which is saved for later retrieval by the {@link #getCause()} method). (A <tt>null</tt> value
	 *           is permitted, and indicates that the cause is nonexistent or unknown.)
	 */
	public DkException(final DkExceptionContext context, final Throwable cause)
	{
		super(context.getMessage(), cause);
		this.context = context;
	}

	/**
	 * Constructs a new GwtLib exception with the specified cause and a detail message of
	 * <tt>(cause==null ? null : cause.toString())</tt> (which typically contains the class and detail message of
	 * <tt>cause</tt>). This constructor is useful for runtime exceptions that are little more than wrappers for other
	 * throwables.
	 * 
	 * @param message
	 *           the exception message
	 * @param cause
	 *           the cause (which is saved for later retrieval by the {@link #getCause()} method). (A <tt>null</tt> value
	 *           is permitted, and indicates that the cause is nonexistent or unknown.)
	 */
	public DkException(final String message, final Throwable cause)
	{
		super(message, cause);
		this.context = new DkExceptionContext(message);
	}

	/**
	 * @return the context
	 */
	public DkExceptionContext getContext()
	{
		return context;
	}

	/**
	 * @param context
	 *           the context to set
	 */
	public void setContext(final DkExceptionContext context)
	{
		this.context = context;
	}

}
